//
//  CustomNavigationController.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/9/6.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "CustomNavigationController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "Search/SearchViewController.h"
#import "NetManager.h"
@interface CustomNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation CustomNavigationController
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.navigationBar.barTintColor = [UIColor colorWithRed:45./255. green:221./255. blue:39./255. alpha:0.5];
    self.navigationBar.barTintColor = [UIColor colorWithRed:252./255. green:150./255. blue:95./255. alpha:1];
    UIFont *font = [UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:20.0];
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName:font,
                                               NSForegroundColorAttributeName:[UIColor whiteColor]
                                               };
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = YES;
}
// 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    return YES;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    viewController.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];
    if(self.viewControllers.count >1 ) {
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        backItem.tintColor = [UIColor whiteColor];
        viewController.navigationItem.leftBarButtonItem = backItem;
    }
}

-(void)back{
    [self popViewControllerAnimated:YES];
}

@end
